<?php
/**
 * @Notes:【】
 * @Date: 2022-06-05 20:03
 */

namespace App\Service\Order;

use App\Controller\Api\Settles\TaskRefreshController;
use App\Dao\Settings\SettingsTaskRefreshDao;
use App\Dao\Settings\SettingsTaskTopDao;
use App\Dao\Task\TaskDao;
use App\Dao\Task\TaskPaymentDao;
use App\Dao\User\UserDao;
use App\Dao\User\WechatUserDao;
use App\Form\Settings\Task\Refresh;
use App\Form\Task;
use App\Model\Task\TaskPayment;
use App\Notice\TaskNotice;
use App\Service\BaseService;
use App\Service\Pay\PayFactory;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

class PayService extends BaseService
{
    /**
     * @Inject()
     * @var TaskPaymentDao
     */
    protected $taskPaymentDao;

    public function task(int $usersId, int $task_id, $PayType)
    {
        $task = App(TaskDao::class)
            ->DaoWhere(['task_id' => $task_id])->first();
        if ($task->pay_status == 1) {
            _ERror('已经支付了');
        }
        $taskPayment = $this->taskPaymentDao->add(_Collect([
            'task_id' => $task_id,
            'user_id' => $usersId,
            'price' => $task->price,
            'types' => 'create'
        ]));

//        /** @var TaskNotice $taskNotice */
//        $taskNotice = app(TaskNotice::class);
//        $taskNotice->handel($taskPayment->id,$taskPayment->price);
//        _SUCCESS();

        if ($PayType == 'balance') {
            //余额
            return $this->getBalancePayment($taskPayment->id, $usersId);
        }

        if ($PayType == 'alipay') {
            //直接支付
            return $this->taskPaymentDao->getAliPayPayment($taskPayment->id);
        }


        $url = $this->taskPaymentDao->getWxPayment($taskPayment->id, $usersId);
        return $url;
    }

    /**
     * @Inject()
     * @var TaskNotice
     */
    protected $taskNotice;

    /**
     * @Inject()
     * @var UserDao
     */
    protected $userDao;

    public function getBalancePayment($taskPaymentId, $usersId)
    {
        /** @var TaskPayment $taskPayment */
        $taskPayment = $this->taskPaymentDao->where('id', $taskPaymentId)->firstOrError();
        $fee = $taskPayment->price;
        Db::beginTransaction();
        try {
            $this->userDao->opAccount('now_money', $usersId, -$fee, 'task');
            $this->taskNotice->handel($taskPaymentId, $fee);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollBack();
            _Error($e->getMessage());
        }
        return true;
    }

    public function settles(mixed $usersId, $refresh_id)
    {
        $settles = App(SettingsTaskRefreshDao::class)->where('id', $refresh_id)->first();
        $taskPayment = $this->taskPaymentDao->add(_Collect([
            'refresh_id' => $refresh_id,
            'user_id' => $usersId,
            'price' => $settles->price,
            'types' => 'refresh'
        ]));
        $url = $this->taskPaymentDao->getWxPayment($taskPayment->id, $usersId);
        return $url;
    }

    public function settlesTop(mixed $usersId, $top_id)
    {
        $settles = App(SettingsTaskTopDao::class)->where('id', $top_id)->first();

        $taskPayment = $this->taskPaymentDao->add(_Collect([
            'top_id' => $top_id,
            'user_id' => $usersId,
            'price' => $settles->price,
            'types' => 'top'
        ]));


        $url = $this->taskPaymentDao->getWxPayment($taskPayment->id, $usersId);
        return $url;
    }


}